package com.nisco.dms.util.pub;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.servlet.view.document.AbstractExcelView;
import com.nisco.dms.entity.Department;

public class PrintExcelUtil extends AbstractExcelView{

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		response.setHeader("Content-Disposition", "inline; filename="+ 
				new String("部门列表".getBytes(), "iso8859-1"));
		
		List<Department> ts = (List<Department>) model.get("depts");
		HSSFSheet sheet = workbook.createSheet("dept");
		
		//设置单元格宽度
		sheet.setColumnWidth(0,  (short) (10 * 256));
		sheet.setColumnWidth(1,  (short) (10 * 256));
		sheet.setColumnWidth(2,  (short) (10 * 256));
		//合并单元格，以后可以通过传参方式设定
		sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
		HSSFRow header = sheet.createRow(0);
		HSSFRow header1 = sheet.createRow(1);
		
		//设置标题高度
		header.setHeight((short) (20*20));
		header1.setHeight((short) (15*20));
		//设置单元格样式
		HSSFCellStyle style = workbook.createCellStyle();
//		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		
		HSSFCellStyle style1 = workbook.createCellStyle();
		//设置大标题字体格式
		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints((short) 16);//字体大小
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //粗体显示
		style.setFont(font);
		
		//设置小标题字体格式
		HSSFFont font1 = workbook.createFont();
		font1.setFontHeightInPoints((short) 12);//字体大小
		font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //粗体显示
		style1.setFont(font1);
		
		//为大标题单元格添加格式
		HSSFCell cell = header.createCell(0);
		cell.setCellStyle(style);
		//设置单元格数据类型
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		//添加大标题，以后可以通过传参方式设定
		cell.setCellValue("部门信息");
		
		HSSFCell cell10 = header1.createCell(0);
		HSSFCell cell11 = header1.createCell(1);
		HSSFCell cell12 = header1.createCell(2);
		
		cell10.setCellStyle(style1);
		cell11.setCellStyle(style1);
		cell12.setCellStyle(style1);
		cell10.setCellValue("部门编码");
		cell11.setCellValue("部门名称");
		cell12.setCellValue("描述");
		
		int rowNum = 2;
		
		for(Department dept : ts){
			HSSFRow row = sheet.createRow(rowNum++);
			
			row.createCell(0).setCellType(HSSFCell.CELL_TYPE_STRING);
			row.createCell(0).setCellValue(dept.getStrSn());
			row.createCell(1).setCellValue(dept.getStrName());
			row.createCell(2).setCellValue(dept.getStrDesc());
		}
	}
	
	
	public  String getModelMap(String param){
		return "";
	}
}
